﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
public partial class Manager_CandidateManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Message"] != null)
        {
            lblmes.Text = Session["Message"].ToString();
            Session.Remove("Message");
        }
        else
            lblmes.Text = "";
        BindCandidate();
    
    }
    protected void grvCandidate_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //string id = grvCandidate.Rows[e.RowIndex].Cells[5].Text;
        string CandidateID = grvCandidate.DataKeys[e.RowIndex].Value.ToString();
        
        bool check;
        check = RegisterCandidate.DeleteCandidate(CandidateID);

        if (check == true)
            Session.Add("Message", "Delete successfully CandidateID : " + CandidateID);
        else
            Session.Add("Message", "<font color='red'>Error : Delete unsuccess CandidateID : " + CandidateID + "</font>");
        Response.Redirect("CandidateManager.aspx");
    }

    protected void BindCandidate()
    {
        DataTable tblData = new DataTable("abc");
        tblData = RegisterCandidate.DisplayCandidate();

        grvCandidate.DataSource = tblData;
        grvCandidate.DataBind();
    }
    protected void grvCandidate_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = grvCandidate.DataKeys[e.RowIndex].Value.ToString();

        Response.Redirect("DetailCandidate.aspx?CanID="+id);
    }
    protected void grvCandidate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        if (e.Row.RowIndex != grvCandidate.EditIndex)
        {
            LinkButton del = (LinkButton)e.Row.Cells[5].FindControl("del");
            del.Attributes.Add("onclick", "return confirm('Delete Candidate?')");
        }
    }
    protected void grvCandidate_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvCandidate.PageIndex = e.NewPageIndex;
        BindCandidate();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string fullname = txtUsername.Text;
        grvCandidate.DataSource = RegisterCandidate.SearchCandidate(fullname);
        grvCandidate.DataBind();
    }
}